//
//  VMCClickableView.m
//  vmcshop
//
//  Created by bigknife on 14/11/7.
//  Copyright (c) 2014年 idongler. All rights reserved.
//

#import "VMCClickableView.h"

@implementation VMCClickableView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self innerInit];
    }
    return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self innerInit];
    }
    return self;
}

-(void) innerInit {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewTapped:)];
    [self addGestureRecognizer:tap];
}

-(void)viewTapped:(id)sender{
    if ([self.delegate respondsToSelector:@selector(tappedInClickableView:)]) {
        [self.delegate tappedInClickableView:self];
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
	//点击时间开始 设置背景颜色高亮（选中状态）
	self.backgroundColor = MainColor;
    if ([self.delegate respondsToSelector:@selector(responseToTouchBegan:)] &&
        [self.delegate respondsToSelector:@selector(touchDidBegin:)]) {
        if ([self.delegate responseToTouchBegan:self]) {
            [self.delegate touchDidBegin:self];
        }
    }
    
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
	self.backgroundColor = [UIColor whiteColor];
    if ([self.delegate respondsToSelector:@selector(responseToTouchBegan:)] &&
        [self.delegate respondsToSelector:@selector(touchDidEnd:)]) {
        if ([self.delegate responseToTouchBegan:self]) {
            [self.delegate touchDidEnd:self];
        }
    }
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesCancelled:touches withEvent:event];
	//点击结束 当前背景颜色设置为正常颜色
	self.backgroundColor = [UIColor whiteColor];
    if ([self.delegate respondsToSelector:@selector(responseToTouchBegan:)] &&
        [self.delegate respondsToSelector:@selector(touchDidCancel:)]) {
        if ([self.delegate responseToTouchBegan:self]) {
            [self.delegate touchDidCancel:self];
        }
    }
}

@end
